#include <Arduino.h>
#include "driver/gpio.h"

#define PIN_NUM_1 9
#define SHORT_PRESS_TIME 100  // 短按时间阈值，单位为毫秒
#define LONG_PRESS_TIME 400  // 长按时间阈值，单位为毫秒

static int count = 0;
static unsigned long pressTime = 0;
static bool isPressed = false;

void setup()
{
  // 初始化串口
  Serial.begin(115200);
  // 配置GPIO
  pinMode(PIN_NUM_1, INPUT_PULLUP);
}

void handleButtonPress()
{
  // 读取GPIO状态
  int buttonState = digitalRead(PIN_NUM_1);

  if (buttonState == 0 && !isPressed)
  {
    // 按键按下
    isPressed = true;
    pressTime = millis();
  }
  else if (buttonState == 1 && isPressed)
  {
    // 按键释放
    isPressed = false;
    unsigned long releaseTime = millis();
    unsigned long pressDuration = releaseTime - pressTime;

    if (pressDuration >= LONG_PRESS_TIME)
    {
      // 长按
      Serial.println("Long press detected");
    }
    else if (pressDuration >= SHORT_PRESS_TIME)
    {
      // 短按
      count++;
      Serial.print("Short press detected, count: ");
      Serial.println(count);
    }
  }
}

void loop()
{
  handleButtonPress();
}